﻿
function TreeView_GetParent(nodeName) {
        try{
        var parentName = document.getElementById(nodeName).parentNode.parentNode.parentNode.parentNode.parentNode.id.replace("Nodes", "");
        return parentName;
        }
        catch(e)
         {return "";}
    }
    function TreeView_HideNodeSiblings(nodeName) {
        index = nodeName.replace('ctl00_ContentPlaceHolder1_TreeView1n', '');
        TreeView_ToggleNode(ctl00_ContentPlaceHolder1_TreeView1_Data, index, document.getElementById(nodeName), 't', document.getElementById(nodeName + 'Nodes'));
        for (i = 0; i < document.getElementById(nodeName).parentNode.parentNode.parentNode.parentNode.parentNode.childNodes.length; i++) {
            isibling = document.getElementById(nodeName).parentNode.parentNode.parentNode.parentNode.parentNode.childNodes[i];
            if (isibling.nodeName != '#text') {//This check is for Firefox
                sibling = isibling.getElementsByTagName("A")[0].id; index = sibling.replace('ctl00_ContentPlaceHolder1_TreeView1n', '');
                if (sibling != nodeName) {
                    //TreeView_CollapseNode(ctl00_ctl00_ContentPlaceHolder1_TreeView1_Data, index, document.getElementById(sibling), '', document.getElementById(sibling + "Nodes"));
                    isibling.style.display = 'none';
                }
            }
        }
    }
    function TreeView_UncheckNodeSiblings(nodeName) {
        for (i = 0; i < document.getElementById(nodeName).parentNode.parentNode.parentNode.parentNode.parentNode.childNodes.length; i++) {
            isibling = document.getElementById(nodeName).parentNode.parentNode.parentNode.parentNode.parentNode.childNodes[i];
            if (isibling.nodeName == "TABLE") {
                sibling = isibling.getElementsByTagName("A")[0].id;
                if (sibling != nodeName) {
                    nodeElement = document.getElementById(sibling + 'CheckBox');
                    if (nodeElement) nodeElement.checked = false;
                } 
            } 
        } 
    }
    function TreeView_UnHideNodeSiblings(nodeName) {
        var childNode;
        var index;
        for (i = 0; i < document.getElementById(nodeName).parentNode.parentNode.parentNode.parentNode.parentNode.childNodes.length; i++) {
            isibling = document.getElementById(nodeName).parentNode.parentNode.parentNode.parentNode.parentNode.childNodes[i];
            if (isibling.nodeName != '#text') {//This check is for Firefox
                childNode = isibling.getElementsByTagName("A")[0].id;
                if (isibling.nodeName == 'TABLE' && childNode != nodeName) { isibling.style.display = 'block'; }
            }
        } 
    }
    function TreeView_GetPathClientFormat(nodeName) {
        nodeText = TreeView_GetNodeText2(document.getElementById(nodeName));
        if (nodeText == 'Quote') return '';
        else {
            parentNode = TreeView_GetParent(nodeName);
            parentText = TreeView_GetNodeText2(document.getElementById(parentNode));
            var path = "\\" + nodeText; path = TreeView_GetPathClientFormat(parentNode) + path;
            return path;
        }
    }

    function TreeView_GetNodeText2(node) {
        if (window.navigator.appName == "Microsoft Internet Explorer") {
            var trNode = WebForm_GetParentByTagName(node, "TR");
            var outerNodes;
            if (trNode.childNodes[trNode.childNodes.length - 1].getElementsByTagName) {
                outerNodes = trNode.childNodes[trNode.childNodes.length - 1].getElementsByTagName("A");
                if (!outerNodes || outerNodes.length == 0) {
                    outerNodes = trNode.childNodes[trNode.childNodes.length - 1].getElementsByTagName("SPAN");
                }
            }
            var textNode = (outerNodes && outerNodes.length > 0) ?
            outerNodes[0].childNodes[0] :
            trNode.childNodes[trNode.childNodes.length - 1].childNodes[0];
            return (textNode && textNode.nodeValue) ? textNode.nodeValue : "";
        }
        else
            if (node != null) {
                var trNode = WebForm_GetParentByTagName(node, "TR");
                var outerNodes;
                if (trNode.childNodes[trNode.childNodes.length - 2].getElementsByTagName) {
                    outerNodes = trNode.childNodes[trNode.childNodes.length - 2].getElementsByTagName("A");
                    if (!outerNodes || outerNodes.length == 0) {
                        outerNodes = trNode.childNodes[trNode.childNodes.length - 2].getElementsByTagName("SPAN");
                    }
                }
                var textNode = (outerNodes && outerNodes.length > 0) ?
            outerNodes[0].childNodes[0] :
            trNode.childNodes[trNode.childNodes.length - 2].childNodes[0];
                return (textNode && textNode.nodeValue) ? textNode.nodeValue : "";

            }
            else return "";
    }

    function TreeView_GetPathServerFormat(nodeName) {
        var nodeText = TreeView_GetNodeText2(document.getElementById(nodeName));

        if (nodeName == "ctl00_ContentPlaceHolder1_TreeView1n0") 
            return TreeView_GetNodeText2(document.getElementById("ctl00_ContentPlaceHolder1_TreeView1n0"));
        else {
            var parentPath = '';
            var parentNode = TreeView_GetParent(nodeName);
            var parentText = TreeView_GetNodeText2(document.getElementById(parentNode));
            var path = nodeText;
            if (parentNode != "") parentPath = TreeView_GetPathServerFormat(parentNode);
            if (parentPath != '') path = parentPath + "/" + path;
            Continue = true;
            do {
                path = path.replace(' ', '%20');
                if (path.search(' ') == -1) Continue = false;
            } 
            while (Continue == true); 
            return path;
        } 
    }
    function replace(string, a, b) {
        result = string;
        Continue = true;
        do {
            result = result.replace(a, b);
            if (result.search(a) == -1) Continue = false;
        }
        while (Continue == true); 
        return result;
    }
    function TreeView_CollapseNode(data, index, node, lineType, children) {
        var img = node.childNodes[0];
        var newExpandState = "c";
        if (children != null) children.style.display = "none";
        if (img != null) {
            if (lineType == "l") { img.src = data.images[14]; }
            else if (lineType == "t") { img.src = data.images[11]; }
            else if (lineType == "-") { img.src = data.images[17]; }
            else { img.src = data.images[4]; }
            img.alt = data.expandToolTip.replace(/\{0\}/, TreeView_GetNodeText2(node));
        }
        data.expandState.value = data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1);
    }
    function TreeView_ExpandNode(data, index, node, lineType, children) {
        var img = node.childNodes[0];
        var newExpandState = "e";
        if (children != null && children.style.display == "none") {
            children.style.display = "block";
            if (img != null) {
                if (lineType == "l") { img.src = data.images[15]; }
                else if (lineType == "t") { img.src = data.images[12]; }
                else if (lineType == "-") { img.src = data.images[18]; }
                else { img.src = data.images[5]; }
                img.alt = data.collapseToolTip.replace(/\{0\}/, TreeView_GetNodeText2(node));
            } 
        }
        data.expandState.value = data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1);
    }
    function createXMLHttpRequest() {
        try { return new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) { }
        try { return new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) { }
        try { return new XMLHttpRequest(); }
        catch (e) { }
        alert("XMLHttpRequest not supported");
        return null;
    }
    function CheckBoxClickHandler(e) {
        if (window.navigator.appName == "Microsoft Internet Explorer") {
            var o = window.event.srcElement;

            if (o.tagName == "INPUT" && o.type == "checkbox" && o.id.search('TreeView') != -1) {
                nodeName = o.id.replace('CheckBox', '');
                index = nodeName.replace('ctl00_ContentPlaceHolder1_TreeView1n', '');
                var setAttributesUrl = '';
                var xhReq = createXMLHttpRequest();
                xhReq.open("GET", "getNodeAttributes.aspx?nodePath=" + TreeView_GetPathServerFormat(nodeName), false);
                xhReq.send(null);
                var serverResponse = xhReq.responseText;
                if (serverResponse == "timeout") {
                    window.location = 'sessionTimeout.aspx';
                    return;
                }
                if (o.checked) {
                    if (serverResponse.search("parentType=decision") != -1) {
                        TreeView_HideNodeSiblings(nodeName);
                        TreeView_UncheckNodeSiblings(nodeName);
                    }
                    if (document.getElementById(nodeName).href.search('TreeView_PopulateNode') != -1) {
                        var jscriptstring = document.getElementById(nodeName).href.replace("javascript:TreeView_PopulateNode(", "").replace(")", "");
                        var array = jscriptstring.split(",");
                        arg0 = eval(array[0]);
                        arg1 = eval(array[1]);
                        arg2 = document.getElementById(array[2]);
                        arg3 = document.getElementById(array[3]);
                        arg5 = replace(replace(array[5], "'", ""), "%20", " ");
                        arg6 = replace(replace(array[6], "'", ""), "%20", " ");
                        arg7 = replace(replace(replace(array[7], "'", ""), "%20", " "), "\\\\", "/");
                        arg7 = replace(arg7, "/", "\\");
                        arg8 = replace(array[8], "'", "");
                        arg9 = replace(array[9], "'", "");
                        arg10 = replace(array[10], "'", "");
                        TreeView_PopulateNode(arg0, arg1, arg2, arg3, null, arg5, arg6, arg7, arg8, arg9, arg10);
                    }
                    else TreeView_ExpandNode(ctl00_ContentPlaceHolder1_TreeView1_Data, index, document.getElementById(nodeName), '', document.getElementById(nodeName + "Nodes"));
                    setAttributesUrl = "setNodeAttributes.aspx?nodePath=" + TreeView_GetPathServerFormat(nodeName) + "&selected=true";
                }
                else {
                    if (serverResponse.search("parentType=decision") != -1) { TreeView_UnHideNodeSiblings(nodeName); }
                    if (document.getElementById(nodeName).href.search('TreeView_ToggleNode') != -1) TreeView_CollapseNode(ctl00_ContentPlaceHolder1_TreeView1_Data, index, document.getElementById(nodeName), '', document.getElementById(nodeName + "Nodes"));
                    setAttributesUrl = "setNodeAttributes.aspx?nodePath=" + TreeView_GetPathServerFormat(nodeName) + "&selected=false";
                }
                var xhReq1 = createXMLHttpRequest();
                xhReq1.open("GET", setAttributesUrl, false);
                xhReq1.send(null);
                var serverResponse1 = xhReq1.responseText;
                document.getElementById("ctl00_ContentPlaceHolder1_Label1").innerText = "Total = $" + serverResponse1;
                frameurl = document.getElementById("ctl00_ContentPlaceHolder1_HyperLink6").href;
                window.frames["ctl00_ContentPlaceHolder1_DescriptionFrame"].location.href = frameurl;
            } 
        }
        else {
            if (!e) e = event;
            var o = e.target || e.srcElement;
            //window.document.write(o.type);
            //window.document.write(o.id);
            if (o.type == "checkbox" && o.id.search('TreeView') != -1) {
                var nodeName = o.id.replace('CheckBox', '');
                var index = nodeName.replace('ctl00_ContentPlaceHolder1_TreeView1n', '');
                var setAttributesUrl = '';
                var xhReq = createXMLHttpRequest();
                xhReq.open("GET", "getNodeAttributes.aspx?nodePath=" + TreeView_GetPathServerFormat(nodeName), false);
                xhReq.send(null);
                var serverResponse = xhReq.responseText;
                if (serverResponse == "timeout") {
                    window.location = 'sessionTimeout.aspx';
                    return;
                }
                if (o.checked) {
                    //window.document.write('The checkbox was checked');
                    if (serverResponse.search("parentType=decision") != -1) {
                        TreeView_HideNodeSiblings(nodeName);
                        TreeView_UncheckNodeSiblings(nodeName);
                    }
                    if (document.getElementById(nodeName).href.search('TreeView_PopulateNode') != -1) {
                        var jscriptstring = document.getElementById(nodeName).href.replace("javascript:TreeView_PopulateNode(", "").replace(")", "");
                        var array = jscriptstring.split(",");
                        var arg0 = eval(array[0]);
                        var arg1 = eval(array[1]);
                        var arg2 = eval(array[2]+')');
                        var arg3 = eval(array[3]);
                        var arg5 = replace(replace(array[5], "'", ""), "%20", " ");
                        var arg6 = replace(replace(array[6], "'", ""), "%20", " ");
                        var arg7 = replace(replace(replace(array[7], "'", ""), "%20", " "), "\\\\", "/");
                        var arg7 = replace(arg7, "/", "\\");
                        var arg8 = replace(array[8], "'", "");
                        var arg9 = replace(array[9], "'", "");
                        var arg10 = replace(array[10], "'", "").replace(')','');
                        TreeView_PopulateNode(arg0, arg1, arg2, arg3, null, arg5, arg6, arg7, arg8, arg9, arg10);
                    }
                    else TreeView_ExpandNode(ctl00_ContentPlaceHolder1_TreeView1_Data, index, document.getElementById(nodeName), '', document.getElementById(nodeName + "Nodes"));
                    setAttributesUrl = "setNodeAttributes.aspx?nodePath=" + TreeView_GetPathServerFormat(nodeName) + "&selected=true";
                }
                else {
                    if (serverResponse.search("parentType=decision") != -1) { TreeView_UnHideNodeSiblings(nodeName); }
                    if (document.getElementById(nodeName).href.search('TreeView_ToggleNode') != -1) TreeView_CollapseNode(ctl00_ContentPlaceHolder1_TreeView1_Data, index, document.getElementById(nodeName), '', document.getElementById(nodeName + "Nodes"));
                    setAttributesUrl = "setNodeAttributes.aspx?nodePath=" + TreeView_GetPathServerFormat(nodeName) + "&selected=false";
                }
                var xhReq1 = createXMLHttpRequest();
                xhReq1.open("GET", setAttributesUrl, false);
                xhReq1.send(null);
                var serverResponse1 = xhReq1.responseText;
                document.getElementById("ctl00_ContentPlaceHolder1_Label1").innerText = "Total = $" + serverResponse1;
                var frameurl = document.getElementById("ctl00_ContentPlaceHolder1_HyperLink6").href;
                window.frames["ctl00_ContentPlaceHolder1_DescriptionFrame"].location.href = frameurl;
            } 
        } 
    }

